package cn.luckdou.commons.utils;

import org.springframework.web.util.HtmlUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexUtils {
    private static String HtmlReg= "<[^>]*>";

    /**
     * 去除所有html标签
     * @param html
     * @return
     */
    public static String  outHtmlLabel(String html){
        if (html==null || html.length()<=0) return html;
        String s = HtmlUtils.htmlUnescape(html);
        s = s.replaceAll(HtmlReg, "");
        return s;
    }

    /**
     *将字符串里面的参数替换成真正的值
     * @param valueMap
     * @param param
     * @return
     */
    public static  String RegexAllParam(Map<String,String> valueMap,String param){
        Set<Map.Entry<String, String>> entries = valueMap.entrySet();
        for (Map.Entry<String, String> entry:entries) {
            param = param.replace(entry.getKey(), entry.getValue());
        }
        return  param;
    }

    /**
     * 获取指定HTML标签的指定属性的值
     *
     * @param source
     *            要匹配的源文本
     * @param element
     *            标签名称
     * @param attr
     *            标签的属性名称，如果属性值为"" 或 NULL 匹配整个标签
     * @param flag
     *            是否为双标签
     * @return
     *            属性值列表
     */
    public static List<String> match(String source, String element, String attr, boolean flag) {
        List<String> result = new ArrayList<String>();
        String reg;
        // 取整个标签
        if (StringUtils.isEmpty(attr)) {
            // 是否为双标签
            if (flag) {
                reg = "<" + element + "(\\s.*?)?</" + element + ">";
                Matcher m = Pattern.compile(reg).matcher(source);
                while (m.find()) {
                    String r = m.group();
                    result.add(r);
                }

            } else {
                reg = "<" + element + "(\\s.*?)?/>";
                Matcher m = Pattern.compile(reg).matcher(source);
                while (m.find()) {
                    String r = m.group();
                    result.add(r);
                }
            }
        } else {
            reg = "<" + element + "[^<>]*?\\s" + attr + "=['\"]?(.*?)['\"]?(\\s.*?)?>";
            Matcher m = Pattern.compile(reg).matcher(source);
            while (m.find()) {
                String r = m.group(1);
                result.add(r);
            }
        }
        return result;
    }

    public static void main(String[] args) {
        String str="<p>\n" +
                "    大是大非上发呆分手的分手的水电费水电费是否是飞蛾晚饭我午饭晚饭&nbsp;\n" +
                "</p>\n" +
                "<p style=\"text-align:center\">\n" +
                "    <img src=\"/upload/image/20171220/1513736381597035474.jpg\" title=\"1513736381597035474.jpg\" alt=\"logo3.jpg\" width=\"181\" height=\"176\"/>\n" +
                "</p>\n" +
                "<p>\n" +
                "    短发短发水电费水电费受到粉丝的似懂非懂说奋斗史水电费水电费时代粉丝的似懂非懂说奋斗史水电费水电费时代粉丝的似懂非懂说奋斗史水电费水电费时代粉丝的似懂非懂说奋斗史水电费水电费时代粉丝的似懂非懂说奋斗史水电费水电费时代粉丝的似懂非懂说奋斗史水电费水电费时代粉丝的似懂非懂说奋斗史水电费水电费时代粉丝的似懂非懂说奋斗史水电费水电费时代粉丝的似懂非懂说奋斗史水电费水电费时代粉丝的似懂非懂说奋斗史水电费水电费时代<img src=\"/upload/image/20171220/1513736472096000616.jpg\" title=\"1513736472096000616.jpg\" alt=\"logo3.jpg\" width=\"175\" height=\"142\"/>很快会尽快很快就会看见很健康<img src=\"/upload/image/20171220/1513736645975085688.jpg\" title=\"1513736645975085688.jpg\" alt=\"ico.jpg\"/>\n" +
                "</p>";
        List<String> img = match(str, "img", "src", false);
        for (String s : img) {
            System.out.println(s);
        }
    }


}
